Lists¶
🎨 Arithmetic Operators¶
7 + 2
9
7 - 2
5
7 * 2
14
7 ** 2
49
7 ** 2
is $ 7 ^ 2 $
7 / 2
3.5
7 // 2
3
7 % 2
1
$\frac{7}{2} = 3r1$
7 // 2
gives us the $3$
7 % 2
gives us the $1$ (the remainder)
🎨 Comparisons¶
2 > 1
True
2 > 2
False
2 <= 2
True
2 >= 2
True
4 == 4
True
4 != 6
True
🎨 None
¶
What is the value of a variable that doesn't have a value?
What does a function return when it doesn't return
anything?
def no_return():
print('This function doesn\'t return anything.')
value = no_return()
print(value)
This function doesn't return anything. None
type(value)
NoneType
None
is what Python uses to communicate nothing.
It's what you use to indicate that you don't have any information.
thing = 8
thing is None
False
thing = None
thing is None
True
To determine whether a variable is None
, use the is None
expression.
thing = 9
thing is not None
True
To determine whether a variable is not None
, use the is not None
expression.
names = []
while True:
name = input("Give me a name: ")
if name == 'q':
break
names.append(name)
print(names)
print(names)
Give me a name: Quinn ['Quinn'] Give me a name: Susan ['Quinn', 'Susan'] Give me a name: Juan ['Quinn', 'Susan', 'Juan'] Give me a name: Janet ['Quinn', 'Susan', 'Juan', 'Janet'] Give me a name: ['Quinn', 'Susan', 'Juan', 'Janet', ''] Give me a name: q ['Quinn', 'Susan', 'Juan', 'Janet', '']
names = ['Julia', 'Juan', 'George', 'Gina']
print(names)
['Julia', 'Juan', 'George', 'Gina']
names = ['Julia', 'Juan', 'George', 'Gina']
while True:
name = input("Give me a name: ")
if name == '':
break
names.append(name)
print(names)
Give me a name: Charles Give me a name: ['Julia', 'Juan', 'George', 'Gina', 'Charles']
🎨 len
¶
fruit = ['apple', 'peach', 'pear', 'açaí']
how_many = len(fruit)
print(how_many)
4
🖌 for
¶
students = ['Julia', 'Juan', 'George', 'Gina', 'Gina']
for name in students:
print(f"Hello {name}. Welcome to CS 110.")
print(f"Sit next to {name}.")
Hello Julia. Welcome to CS 110. Sit next to Julia. Hello Juan. Welcome to CS 110. Sit next to Juan. Hello George. Welcome to CS 110. Sit next to George. Hello Gina. Welcome to CS 110. Sit next to Gina. Hello Gina. Welcome to CS 110. Sit next to Gina.
👩🏽🎨 Big and Small¶
Write a program that queries the user for a list of numbers (one number at a time).
Then ask the user what number to use as the boundary between "big" and "small" numbers.
Print "You have {how_many} numbers"
Then print the small numbers with the heading "These are small:"
Then print the bit numbers with the heading "These are big:"
Number: 8 Number: 10 Number: 30 Number: 2 Number: 65 Number: 42 Number: 13 Number: 77 Number: Boundary: 20 You have 8 numbers These are small: 8 10 2 13 These are big: 30 65 42 77
Key Ideas¶
- Arithmetic operators
- Lists!
[]
.append(...)
len
for